﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Serialization;


namespace QA_System
{
    class MergeClass
    {
        static private XMLNounConnection nounconn = new XMLNounConnection();

        static private XMLVerbConnection verbconn = new XMLVerbConnection();

        static public bool iresolve = true;

        static private sentence_tagger sen = null;

        static private sentence_tagger pre1 = null;

        static private sentence_tagger pre2 = null;
        //static private sentence_tagger[] pre3 = new sentence_tagger[];
        //static int k = 0;

        static private NPAnaphoraDisCover disNPana = new NPAnaphoraDisCover();

        static private OneAnaphoraDisCover disOneana = new OneAnaphoraDisCover();

        static private SPAnaphoraDisCover disSPana = new SPAnaphoraDisCover();

        static private VPAnaphoraDisCover disVPana = new VPAnaphoraDisCover();

        static private NN_EcllipsisDiscover disellip = new NN_EcllipsisDiscover();

        static private VP_EcllipsisDiscover disellipVp = new VP_EcllipsisDiscover();


        static public bool Merge(ref Stack<sentence_tagger> inStack)
        {
            try
            {
                iresolve = true;
                sentence_tagger resultSen = null;
                if (inStack.Count > 0)
                {
                    bool test = false;
                    bool ok = false;
                    #region
                    //sen = inStack.Pop();
                    //pre1 = inStack.Pop();
                    //SentenceStruct ssSen = new SentenceStruct(sen);
                    //SentenceStruct ssPresen = new SentenceStruct(pre1);
                    /*
                    #region OneAnaphora
                    test = disOneana.IsOccup(ssSen);
                    if (test)
                    {
                        ok = false;
                        resultSen = OneAnaphoraResolve.Resolve(ssPresen,ssSen);
                        ok = OneAnaphoraResolve.Iresolve;
                        if (ok)
                        {
                            ssSen = new SentenceStruct(resultSen);
                            sen = resultSen;
                        }
                    }

                    #endregion
                    #region SPanaphora
                    /*
                    test = disSPana.IsOccup(ssSen);
                    if (test)
                    {
                        resultSen = SAnaphoraResolve.Resolve(ssPresen, ssSen);
                        ok = SAnaphoraResolve.Iresolve;
                        if ((!ok) || (null == resultSen))
                        {
                            iresolve = false;
                            inStack.Push(pre1);
                            inStack.Push(sen);
                            return false;
                        }
                        else
                        {
                           
                            iresolve = true;
                            ssSen = new SentenceStruct(resultSen);
                            sen = resultSen;
                        }
                    }
                     #endregion
                     * */
                    #endregion



                    #region NPanaphora
                    //test = disNPana.IsOccup(ssSen);
                    test = true;
                    if (test)
                    {
                        iresolve = false;
                        sen = inStack.Pop();
                        //pre1 = inStack.Pop();
                        SentenceStruct ssSen = new SentenceStruct(sen);
                        test = disNPana.IsOccup(ssSen);
                        if ((test) && (inStack.Count > 0))
                        {
                            sentence_tagger[] pre3 = new sentence_tagger[inStack.Count()];
                            int k = 0;

                            while ((inStack.Count > 0))
                            {
                                if ((!disNPana.IsOccup(ssSen)))
                                {
                                    break;
                                }
                                else
                                {
                                    pre2 = inStack.Pop();

                                    pre3[k] = pre2;
                                    k++;
                                    SentenceStruct ssPresen2 = new SentenceStruct(pre2);
                                    resultSen = NPAnaphoraResolve.Resolve(ssPresen2, ssSen, inStack.Count);
                                    SentenceStruct testResult = null;
                                    if (resultSen != null)
                                    {
                                        testResult = new SentenceStruct(resultSen);
                                    }
                                    ok = NPAnaphoraResolve.Iresolve;
                                    if (inStack.Count == 0)
                                    {
                                        iresolve = false;
                                        for (int m = 0; m < k; m++)
                                        {
                                            if (pre3[m] != null)
                                                inStack.Push(pre3[m]);
                                        }

                                        if (resultSen != null)
                                            inStack.Push(resultSen);
                                        else
                                            inStack.Push(sen);

                                        break;

                                    }

                                    else if (ok && ((resultSen != null) && (!disNPana.IsOccup(testResult))))
                                    {
                                        iresolve = true;
                                        for (int m = 0; m < k; m++)
                                        {
                                            if (pre3[m] != null)
                                                inStack.Push(pre3[m]);
                                        }

                                        inStack.Push(resultSen);
                                        break;
                                    }
                                }
                            }
                        }
                        else if (disellip.IsOccup(ssSen))
                        {
                            #region NN_Ellip

                            test = disellip.IsOccup(ssSen);
                            if (test)
                            {
                                iresolve = false;
                                pre1 = inStack.Pop();
                                SentenceStruct ssPresen = new SentenceStruct(pre1);
                                //inStack.Push(pre1);
                                if (sen[sen.Sentence_length - 1].Word_pos.Equals("."))
                                {
                                    resultSen = NN_EcllipsisResolve.Resolve(ssPresen, ssSen);
                                    ok = NN_EcllipsisResolve.Iresolve;
                                    if ((!ok) || (resultSen == null))
                                    {
                                        iresolve = false;
                                        inStack.Push(pre1);
                                        inStack.Push(sen);
                                        return false;
                                    }
                                    else
                                    {
                                        iresolve = true;
                                        ssSen = new SentenceStruct(resultSen);
                                        sen = resultSen;
                                        inStack.Push(pre1);
                                        inStack.Push(sen);
                                        return true;
                                    }
                                }
                                else
                                {
                                    if (inStack.Count > 0)
                                    {
                                        pre2 = inStack.Pop();
                                        if (pre2 != null)
                                        {
                                            ssPresen = new SentenceStruct(pre2);
                                            resultSen = NN_EcllipsisResolve.Resolve(ssPresen, ssSen);
                                            ok = NN_EcllipsisResolve.Iresolve;
                                            if ((!ok) || (resultSen == null))
                                            {
                                                iresolve = false;
                                                inStack.Push(pre2);
                                                inStack.Push(pre1);
                                                inStack.Push(sen);
                                                return false;
                                            }
                                            else
                                            {
                                                iresolve = true;
                                                ssSen = new SentenceStruct(resultSen);
                                                sen = resultSen;
                                                inStack.Push(pre2);
                                                inStack.Push(pre1);
                                                inStack.Push(sen);
                                                return true;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        iresolve = false;
                                        inStack.Push(pre1);
                                        inStack.Push(sen);
                                        return false;
                                    }
                                }
                            }


                            #endregion
                        }
                        else
                        {
                            inStack.Push(sen);
                            while (inStack.Count > 0)
                            {
                                break;
                            }
                        }
                    }
                    #endregion
                }
                #region
                /*
                    #region VP_ellip
                    test = disellipVp.IsOccup(ssSen);
                    if (test)
                    {
                        resultSen = VP_EcllipsisResolve.Resolve(ssPresen, ssSen);
                        ok = VP_EcllipsisResolve.Iresolve;
                        if ((!ok) || (resultSen == null))
                        {
                            if (inStack.Count > 0)
                            {
                                pre2 = inStack.Pop();
                                SentenceStruct ssPresen2 = new SentenceStruct(pre2);
                                resultSen = VP_EcllipsisResolve.Resolve(ssPresen2, ssSen);
                                ok = VP_EcllipsisResolve.Iresolve;
                                if ((!ok) || (resultSen == null))
                                {
                                    iresolve = false;
                                    inStack.Push(pre2);
                                    inStack.Push(pre1);
                                    inStack.Push(sen);
                                    //return false;
                                }
                                else
                                {

                                    iresolve = true;
                                    ssSen = new SentenceStruct(resultSen);
                                    sen = resultSen;
                                }
                            }
                            else
                            {
                                iresolve = false;
                                inStack.Push(pre1);
                                inStack.Push(sen);
                                //return false;
                            }
                        }
                        else
                        {
                            iresolve = true;
                            ssSen = new SentenceStruct(resultSen);
                            sen = resultSen;
                        }
                    }
                    #endregion

                    #region VPanaphora
                    test = disVPana.IsOccup(ssSen);
                    if (test)
                    {
                        resultSen = VPAnaphoraResolve.Resolve(ssPresen, ssSen);
                        ok = VPAnaphoraResolve.Iresolve;
                        if ((!ok) || (resultSen == null))
                        {
                            if (inStack.Count > 0)
                            {
                                pre2 = inStack.Pop();
                                SentenceStruct ssPresen2 = new SentenceStruct(pre2);
                                resultSen = VPAnaphoraResolve.Resolve(ssPresen2, ssSen);
                                ok = VPAnaphoraResolve.Iresolve;
                                if ((!ok) || (resultSen == null))
                                {
                                    iresolve = false;
                                    inStack.Push(pre2);
                                    inStack.Push(pre1);
                                    inStack.Push(sen);
                                    return false;
                                }
                                else
                                {

                                    iresolve = true;
                                    ssSen = new SentenceStruct(resultSen);
                                    sen = resultSen;
                                }
                            }
                            else
                            {

                                /////
                                iresolve = false;
                                inStack.Push(pre1);
                                inStack.Push(sen);
                                return false;
                            }
                        }
                        else
                        {
                            iresolve = true;
                            ssSen = new SentenceStruct(resultSen);
                            sen = resultSen;
                        }
                    }
                     * */

                    #endregion

                //inStack.Push(pre1);
                //if (resultSen != null) inStack.Push(resultSen);
                //else { inStack.Push(sen); }


                //}
                return iresolve;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
